package 力扣_树算法.遍历.层序遍历扩展;


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**515. 在每个树行中找最大值
 * @author zx
 * @create 2022-04-09 18:28
 */
public class 在每个树行中找最大值_515 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int levelCount = queue.size();
            int max = Integer.MIN_VALUE;
            for(int i = 0;i < levelCount;i++){
                TreeNode node = queue.poll();
                max = Math.max(max,node.val);
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
            }
            res.add(max);
        }
        return res;
    }
}
